﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;

namespace SelfService.Controllers
{
    [AuthorizeJwtWithCookie]
    [ActionDescription("百度AI")]
    [ApiController]
    [Route("api/Baidu")]
    public class BaiduController : BaseApiController
    {
        private string ApiKey = "ewS7IfRlg6D5QcsK3v2u86HN";
        private string SecretKey = "0cxNu4WlcwYItAxEyb19ozQBwDRzeM1l";

        [HttpGet("SpeechSynthesis/{content}")]
        [Public]
        public ActionResult SpeechSynthesis(string content)
        {
            var client = new Baidu.Aip.Speech.Tts(ApiKey, SecretKey) { Timeout = 60000 };
            var option = new Dictionary<string, object>
            {
                {"spd", 4}, // 语速, 最大9
                {"vol", 6}, // 音量, 最大15
                {"per", 4}  // 发音人
            };
            var result = client.Synthesis(content, option);
            if (result.ErrorCode == 0)  // 或 result.Success
            {
                string fileName = Guid.NewGuid().ToString();
                return File(result.Data, "audio/mp3", $"{fileName}.mp3");
            }
            return BadRequest(result.ErrorMsg);
        }

        [HttpGet("SpeechSynthesisV2")]
        [Public]
        public ActionResult SpeechSynthesisV2([FromQuery]string content, [FromQuery]int spd = 6, [FromQuery]int vol = 6, [FromQuery]int per = 4)
        {
            var client = new Baidu.Aip.Speech.Tts(ApiKey, SecretKey) { Timeout = 60000 };
            var option = new Dictionary<string, object>
            {
                {"spd", spd}, // 语速, 最大9
                {"vol", vol}, // 音量, 最大15
                {"per", per}  // 发音人
            };
            var result = client.Synthesis(content, option);
            if (result.ErrorCode == 0)  // 或 result.Success
            {
                string fileName = Guid.NewGuid().ToString();
                return File(result.Data, "audio/mp3", $"{fileName}.mp3");
            }
            return BadRequest(result.ErrorMsg);
        }
    }
}
